<?php
namespace Pw\MainBundle\Classes;
class HeroTag extends Tag
{
	//display: block
	//margin: auto
	const IMAGE_PATH = '/images/heroes/';
	private $em;
	private $router;
	private $baseUrl;
	public function __construct($em, $baseUrl, $router){
		parent::__construct('hero', null, null, false, array('type' => array('icon', 'art', 'text')));
		$this->em = $em;
		$this->router = $router;
		$this->baseUrl = $baseUrl . self::IMAGE_PATH;
	}
	public function isParamAllowed($param, $value){
		if (parent::isParamAllowed($param, $value) || ($param === '_self'/* && is_numeric($value)*/)) {
			return true;
		}
		return false;
	}
	public function getRepalceOpen() {
		$res = '';
		//$hero = $this->em->getRepository('PwMainBundle:Hero')->find($id);
		if (isset($this->params['_self'])) {
			//$hero = $this->em->getRepository('PwMainBundle:Hero')->find((int)$this->params['_self']);
			$query = $this->em->createQuery('SELECT h FROM PwMainBundle:Hero h WHERE h.name = :name AND h.visible=1')->setParameter('name', $this->params['_self']);
			$result = $query->getResult();
			$hero = false;
			if (isset($result[0])) {
				$hero = $result[0];
			}			
			if ($hero && isset($this->params['type'])) {
				
				switch ($this->params['type']){
				case 'icon':
					$res .= '<img class="iconSmall" herotooltip="' . $hero->getId() . '" src="' . $this->baseUrl . $hero->getIconName() . '" />';
					break;
				case 'art':
					$res .= '<img herotooltip="' . $hero->getId() . '" src="' . $this->baseUrl . $hero->getArtName() . '" />';
					break;
				case 'text':
					$res .= '<a herotooltip="' . $hero->getId() . '" href="' . $this->router->generate('hero_show', array('heroName' => $hero->getName())) . '">' . $hero->getName() . '</a>';
					break;
				}
			}
		}
		return $res;
	}
	public function getRepalceClose() {
		return '';
	}
}
